package com.yong.ssj.db.dao.order;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.mchange.v2.util.XORShiftRandomUtils;
import com.yong.ssj.db.dto.cost.CostMethodDto;
import com.yong.ssj.db.dto.customer.MaterialDto;
import com.yong.ssj.db.dto.order.OrderDto;
import com.yong.ssj.db.dto.order.OrderOperateDto;
import com.yong.ssj.db.dto.order.ReturnApplyDto;
import com.yong.ssj.util.DateTimeUtils;
import com.yong.ssj.vo.order.OrderOperateVo;

@Repository(value="OrderDaoImpl")
public class OrderDaoImpl implements IOrderDao{
	@Resource
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public List<Map<String, Object>> queryOrder(String order_status) {
		// TODO Auto-generated method stub
		String sql = "select a.*,b.customer_name from tb_order a,tb_customer b where a.order_status=? and a.customer_id=b.customer_id";
		List<Map<String, Object>> list =  jdbcTemplate.queryForList(sql,new Object[]{order_status});
		return list;
	}

	@Override
	public List<Map<String, Object>> queryReturn(String return_status) {
		// TODO Auto-generated method stub
		String sql = "select a.*,b.customer_name,c.principal,c.receive_method,d.*,DATEDIFF(a.return_time,c.apply_time) AS DiffDate from tb_return_apply a,tb_customer b,tb_order c,tb_cost_method d where a.return_status=? and a.order_id=c.order_id and c.customer_id=b.customer_id and d.cost_id=c.cost_id";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql,new Object[]{return_status});
		return list;
	}

	@Override
	public OrderOperateDto queryOrderOperate(long order_id) {
		// TODO Auto-generated method stub
		String sql = "select * from tb_order_operate where order_id =?";
		OrderOperateDto dto = jdbcTemplate.queryForObject(sql,  new Object[]{order_id}, new BeanPropertyRowMapper<OrderOperateDto>(OrderOperateDto.class));
		return dto;
	}

	@Override
	public int updateOrderStatus(String order_status,long order_id,double money) {
		// TODO Auto-generated method stub
		String sql = "update tb_order set order_status=?,principal=? where order_id=?";
		int yan = jdbcTemplate.update(sql,new Object[]{order_status,money,order_id});
		return yan;
	}

	@Override
	public int updateOrderStatusf(String order_status,long order_id,String method) {
		// TODO Auto-generated method stub
		String sql = "update tb_order set order_status=?,receive_method=? where order_id=?";
		int yan = jdbcTemplate.update(sql,new Object[]{order_status,method,order_id});
		return yan;
	}
	
	@Override
	public int updateOrderBorrow(OrderOperateVo vo) {
		// TODO Auto-generated method stub
		StringBuffer sql = new StringBuffer("insert into tb_order_operate(");
		sql.append("operate_type,operate_time,operate_account_type,operate_account_code,");
		sql.append("customer_account_type,customer_account_code,pz_file,user_id,order_id,return_apply_id) values(?,?,?,?,?,?,?,?,?,?)");
		List<Object> args = new ArrayList<Object>();
		args.add(vo.getOperate_type());
		args.add(vo.getOperate_time());
		args.add(vo.getOperate_account_type());
		args.add(vo.getOperate_account_code());
		args.add(vo.getCustomer_account_type());
		args.add(vo.getCustomer_account_code());
		args.add(vo.getPz_file());
		args.add(vo.getUser_id());
		args.add(vo.getOrder_id());
		args.add(vo.getReturn_apply_id());
		int yan = jdbcTemplate.update(sql.toString(),args.toArray());
		return yan;
	}

	@Override
	public int updateReturnStatus(String return_status,OrderOperateVo vo) {
		// TODO Auto-generated method stub
		String sql = "update tb_return_apply set return_status=?,return_time=?,return_method=? where return_apply_id=?";
		int yan = jdbcTemplate.update(sql,new Object[]{return_status,vo.getOperate_time(),vo.getCustomer_account_type(),vo.getReturn_apply_id()});
		return yan;
	}

	@Override
	public List<Map<String, Object>> queryBorrow(String customer_name) {
		// TODO Auto-generated method stub
		String sql = "select a.*,b.customer_name from tb_order a,tb_customer b where b.customer_name=? and a.customer_id=b.customer_id";
		List<Map<String, Object>> list =  jdbcTemplate.queryForList(sql,new Object[]{customer_name});
		return list;
	}

	@Override
	public List<Map<String, Object>> queryReturnph(String customer_name) {
		// TODO Auto-generated method stub
		String sql = "select a.*,b.customer_name,c.apply_money,c.receive_method,d.*,DATEDIFF(a.return_time,c.apply_time) AS DiffDate from tb_return_apply a,tb_customer b,tb_order c,tb_cost_method d where b.customer_name=? and a.order_id=c.order_id and c.customer_id=b.customer_id and d.cost_id=c.cost_id";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql,new Object[]{customer_name});
		return list;
	}

	@Override
	public List<Map<String, Object>> queryBo(long order_id) {
		// TODO Auto-generated method stub
		String sql = "select a.customer_name,b.apply_money,c.* from tb_customer a,tb_order b,tb_order_operate c where c.order_id=b.order_id and b.order_id=? and a.customer_id=b.customer_id";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql,new Object[]{order_id});
		return list;
	}

	@Override
	public List<Map<String, Object>> queryRe(long order_id) {
		// TODO Auto-generated method stub
		String sql = "select a.*,b.*,c.* from tb_customer a,tb_return_apply b,tb_order_operate c,tb_order d  where b.return_apply_id=? and b.return_apply_id=c.return_apply_id and d.order_id=b.order_id and d.customer_id=a.customer_id";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql,new Object[]{order_id});
		return list;
	}

	@Override
	public int updateHuanmoney(double rz_surplus_money,long customer_id) {
		// TODO Auto-generated method stub
		String sql="update tb_material set rz_surplus_money=? where customer_id=?";
		int yan = jdbcTemplate.update(sql,new Object[]{rz_surplus_money,customer_id});
		return yan;
	}

	@Override
	public List<Map<String, Object>> queryFangmoney(long order_id) {
		// TODO Auto-generated method stub
		String sql = "select a.rz_surplus_money,b.apply_money,a.customer_id from tb_material a,tb_order b where b.order_id=? and b.customer_id=a.customer_id";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql,new Object[]{order_id});
		return list;
	}

	@Override
	public List<Map<String, Object>> queryCost() {
		// TODO Auto-generated method stub
		String sql = "select * from tb_cost_method";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}

	@Override
	public int insertBorrow(double apply_money,long cost_id,long customer_id) {
		// TODO Auto-generated method stub
		String sql = "insert into tb_order(customer_id,cost_id,apply_money,apply_time,order_status,principal) values(?,?,?,?,?,?)";
        String dateDir = DateTimeUtils.formatDate(new Date());
		int yan=jdbcTemplate.update(sql, new Object[]{customer_id,cost_id,apply_money,dateDir,"0",apply_money});
		return yan;
	}

	@Override
	public List<Map<String, Object>> queryborrowList(long customer_id) {
		// TODO Auto-generated method stub
		String sql = "select * from tb_order where customer_id=? and order_status=?";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql,new Object[]{customer_id,"0"});
		return list;
	}

	@Override
	public List<Map<String, Object>> queryborrowedList(long customer_id) {
		// TODO Auto-generated method stub
		String sql = "select * from tb_order where customer_id=? and order_status=?";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql,new Object[]{customer_id,"1"});
		return list;
	}

	@Override
	public List<Map<String, Object>> queryhuanList(long customer_id) {
		// TODO Auto-generated method stub
		String sql = "select a.*,b.* from tb_order a,tb_return_apply b where a.customer_id=? and a.order_id=b.order_id";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql,new Object[]{customer_id});
		return list;
	}

	@Override
	public List<Map<String, Object>> querytiqianList(long order) {
		// TODO Auto-generated method stub
		String sql = "select a.*,b.* from tb_order a,tb_cost_method b where order_id=? and a.cost_id=b.cost_id";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql,new Object[]{order});
		return list;
	}

	@Override
	public int querytiqianedList(long order,double money) {
		// TODO Auto-generated method stub
		String sql = "insert into tb_return_apply(order_id,return_status,appointment_time,return_kinds,return_money) values(?,?,?,?,?)";
		String time = DateTimeUtils.formatDate(new Date());
		int yan=jdbcTemplate.update(sql, new Object[]{order,"0",time,"1",money});
		return yan;
	}

	@Override
	public List<Map<String, Object>> queryShould() {
		// TODO Auto-generated method stub
		String sql = "select a.*,b.* from tb_order a,tb_cost_method b where a.cost_id=b.cost_id and order_status=?";
		List<Map<String, Object>> list =  jdbcTemplate.queryForList(sql,new Object[]{"1"});
		return list;
	}

	@Override
	public int insertReturn(long order_id, double return_money, String return_status, String return_kinds) {
		// TODO Auto-generated method stub
		String sql = "insert into tb_return_apply(order_id,return_status,appointment_time,return_kinds,return_money) values(?,?,?,?,?)";
		String time = DateTimeUtils.formatDate(new Date());
		int yan=jdbcTemplate.update(sql, new Object[]{order_id,return_status,time,return_kinds,return_money});
		return yan;
	}

	@Override
	public List<Map<String, Object>> queryhuan(long return_apply_id) {
		// TODO Auto-generated method stub
		String sql = "select a.*,b.*,c.* from tb_return_apply a,tb_order b,tb_material c where a.return_apply_id=? and a.order_id=b.order_id and b.customer_id=c.customer_id";
		List<Map<String, Object>> list =  jdbcTemplate.queryForList(sql,new Object[]{return_apply_id});
		return list;
	}

	

}
